﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Data;
//using System.Reflection;
//using System.Data.SqlClient;


//namespace Centrify.DirectAudit.GatewayDatabase
//{
//    public class GatewayStoredProcedure<T, TInput, TOutput, TVariables> : Player.Base.StoredProcedure<T, TInput, TOutput, TVariables>
//        where TInput : struct
//        where TOutput : struct
//        where TVariables : struct
//    {
//        protected static bool IsDatabaseOwner
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.IsDatabaseOwner;
//            }
//        }

//        protected static bool IsManagementDatabaseAdministrator
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.IsManagementDatabaseAdministrator();
//            }
//        }

//        protected static bool IsQueryOwner(int queryId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.IsQueryOwner(queryId);
//        }

//        protected static bool IsUser
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.IsUser;
//            }
//        }

//        protected static byte[] UserSid
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.UserSid;
//            }
//        }

//        protected static string UserName
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.UserName;
//            }
//        }

//        protected static string LoginToken
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.LoginToken;
//            }
//        }

//        protected static string GetAuditStoreDatabaseConnectionString(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.GetAuditStoreDatabaseConnectionStringById(auditStoreDatabaseId);
//        }

//        protected static string GetAuditStoreDatabaseConnectionString(string server, string database)
//        {
//            AccessRight helper = new AccessRight();
//            // do not use the connection pool here as the audit server may not have permission to connect to the 
//            // audit store database now. If this connection is pooled, we won't be able to connect to the audit store
//            // database via audit server even the audit server has granted permission on the audit store database later
//            return helper.GetAuditStoreDatabaseConnectionString(server, database, false);
//        }

//        protected static AuditStoreDatabaseType GetAuditStoreDatabaseTypeById(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.GetAuditStoreDatabaseTypeById(auditStoreDatabaseId);
//        }

//        protected static bool HasActiveAuditStoreDatabase(int auditStoreId)
//        {
//            bool result = false;
//            string sql = Build("SELECT", AuditStore.ActiveDatabaseId, "FROM", typeof(AuditStore),
//                "WHERE", AuditStore.Id, "=", auditStoreId.ToString());
//            using (SqlConnection connection = new SqlConnection(SqlHelper.CONTEXT_CONNECTION))
//            {
//                connection.Open();
//                using (SqlCommand command = CreateCommand(connection, sql))
//                {
//                    object queryResult = command.ExecuteScalar();
//                    int id = queryResult == null ? 0 : (int)queryResult;
//                    result = (id > 0);

//                    connection.Trace("HasActiveAuditStoreDatabase result: {0}", result);
//                }
//            }
//            return result;
//        }

//        /// <summary>
//        /// Manipulates DirectAudit permission.
//        /// </summary>
//        protected class Permission
//        {
//            /// <summary>
//            /// Gets the trustee SIDs from existing permission output list.
//            /// </summary>
//            /// <typeparam name="TPermissionOutput">The type of the permission output.</typeparam>
//            /// <param name="existingPermissions">The existing permissions output list.</param>
//            /// <param name="getPermissionTrusteeSidAction">The delegate to get permission trustee SID.</param>
//            public static void GetExistingTrusteeSids<TPermissionOutput>(
//                IEnumerable<TPermissionOutput> existingPermissionOutputs,
//                Action<TPermissionOutput> getPermissionTrusteeSidAction)
//                where TPermissionOutput : struct
//            {
//                foreach (TPermissionOutput existingPermissionOutput in existingPermissionOutputs)
//                {
//                    getPermissionTrusteeSidAction(existingPermissionOutput);
//                }
//            }

//            /// <summary>
//            /// Gets the trustee SIDs removed from the permission list.
//            /// </summary>
//            /// <param name="newAces">The new list of ACEs.</param>
//            /// <param name="existingTrusteeSids">The trustee sids of existing permission list.</param>
//            /// <returns>The trustee SIDs removed from the permission list.</returns>
//            public static IList<byte[]> GetRemovedTrusteeSids(
//                AceEntry[] newAces,
//                IList<byte[]> existingTrusteeSids)
//            {
//                List<byte[]> results = new List<byte[]>(existingTrusteeSids);

//                // Remove user which is in the new permission list
//                results.RemoveAll(
//                    p => Array.Exists(
//                        newAces,
//                        e => BitConverter.ToString(e.TrusteeSid)
//                            .Equals(BitConverter.ToString(p), StringComparison.InvariantCultureIgnoreCase)));

//                return results;
//            }

//            /// <summary>
//            /// Removes the user accounts identified by the specified trustee SIDs.
//            /// </summary>
//            /// <param name="trusteeSids">The trustee SIDs.</param>
//            public static void RemoveUserAccounts(IList<byte[]> trusteeSids)
//            {
//                foreach (byte[] trusteeSid in trusteeSids)
//                {
//                    // Check whether removed user still has been assigned any other DA permissions
//                    if (!AnyAssignedPermission(trusteeSid))
//                    {
//                        // If the removed user is not assigned any other DA permission, remove user from assigned role...
//                        UserAccountRemove.Process(trusteeSid);
//                    }
//                }
//            }
//        }

//        /// <summary>
//        /// Check whether there is any permission assigned to the specified trustee.
//        /// </summary>
//        /// <param name="trusteeSid">The trustee SID.</param>
//        /// <returns>true if there is any permission assigned to the specified trustee; false otherwise.</returns>
//        private static bool AnyAssignedPermission(byte[] trusteeSid)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.AnyAssignedPermission(trusteeSid);
//        }

//        #region Installation Rights

//        protected static bool CanChangeInstallationPermission
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanChangeInstallationPermission;
//            }
//        }

//        protected static bool CanModifyInstallationName
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanModifyInstallationName;
//            }
//        }

//        protected static bool CanManageManagementDatabaseList
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageManagementDatabaseList;
//            }
//        }

//        protected static bool CanManageAuditStoreList
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageAuditStoreList;
//            }
//        }

//        protected static bool CanManageCollector
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageCollector;
//            }
//        }

//        protected static bool CanManageAuditedMachine
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageAuditedMachine;
//            }
//        }

//        protected static bool CanManageQuery
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageQuery;
//            }
//        }

//        protected static bool CanManagePublication
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManagePublication;
//            }
//        }

//        protected static bool CanManageLicense
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageLicense;
//            }
//        }

//        protected static bool CanPerformAudit
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanPerformAudit;
//            }
//        }

//        protected static bool CanUpdatePublication
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanUpdatePublication;
//            }
//        }

//        protected static bool CanManageAuditRole
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageAuditRole;
//            }
//        }

//        #endregion

//        #region Audit Server Permission

//        protected static bool CanChangeManagementDatabasePermission()
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanChangeManagementDatabasePermission();
//        }

//        protected static bool CanModifyDatabaseName()
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanModifyDatabaseName();
//        }

//        protected static bool CanManageSite()
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageSite();
//        }

//        protected static bool CanRemoveDatabase()
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanRemoveDatabase();
//        }

//        protected static bool CanManageSQLLogin
//        {
//            get
//            {
//                AccessRight helper = new AccessRight();
//                return helper.CanManageSQLLogin;
//            }
//        }

//        protected static bool CanManageTrace()
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageTrace();
//        }

//        protected static bool CanChangeManageDatabaseAdministrator()
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanChangeManagementDatabaseAdministrator();
//        }

//        protected static bool CanChangeAuditStoreAdministrator(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanChangeAuditStoreAdministrator(auditStoreId);
//        }

//        #endregion

//        #region Audit Store Permission

//        protected static bool CanChangeAuditStorePermission(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanChangeAuditStorePermission(auditStoreId);
//        }

//        protected static bool CanModifyAuditStoreName(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanModifyAuditStoreName(auditStoreId);
//        }

//        protected static bool CanManageAuditStoreSite(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreSite(auditStoreId);
//        }

//        protected static bool CanRemoveAuditStore(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanRemoveAuditStore(auditStoreId);
//        }

//        protected static bool CanManageAuditStoreSQLLogin(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreSQLLogin(auditStoreId);
//        }

//        protected static bool CanManageAuditStoreTrace(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreTrace(auditStoreId);
//        }

//        protected static bool CanManageAuditStoreCollector(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreCollector(auditStoreId);
//        }

//        protected static bool CanManageAuditStoreAuditedMachine(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreAuditedMachine(auditStoreId);
//        }

//        protected static bool CanManageAuditStoreDatabases(int auditStoreId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreDatabases(auditStoreId);
//        }

//        #endregion

//        #region Audit Store Database Permission

//        protected static bool CanModifyAuditStoreDatabaseName(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanModifyAuditStoreDatabaseName(auditStoreDatabaseId);
//        }

//        protected static bool CanRemoveAuditStoreDatabase(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanRemoveAuditStoreDatabase(auditStoreDatabaseId);
//        }

//        protected static bool CanManageAuditStoreDatabaseCollector(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreDatabaseCollector(auditStoreDatabaseId);
//        }

//        protected static bool CanManageAuditStoreDatabaseAuditedMachine(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreDatabaseAuditedMachine(auditStoreDatabaseId);
//        }

//        protected static bool CanManageAuditStoreDatabaseSQLLogin(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreDatabaseSQLLogin(auditStoreDatabaseId);
//        }

//        protected static bool CanManageAuditStoreDatabaseTrace(int auditStoreDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanManageAuditStoreDatabaseTrace(auditStoreDatabaseId);
//        }

//        #endregion

//        #region Query Permission

//        protected static bool CanReadQuery(int queryId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanReadQuery(queryId);
//        }

//        protected static bool CanChangeQueryPermission(int queryId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanChangeQueryPermission(queryId);
//        }

//        protected static bool CanDeleteQuery(int queryId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanDeleteQuery(queryId);
//        }

//        protected static bool CanModifyQuery(int queryId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.CanModifyQuery(queryId);
//        }
//        #endregion

//        #region Audit Role Permission

//        protected static bool CanChangeAuditRolePermission(int roleId)
//        {
//            AccessRight accessRight = new AccessRight();
//            return accessRight.CanChangeAuditRolePermission(roleId);
//        }

//        protected static bool CanChangeAuditRoleDefinition(int roleId)
//        {
//            AccessRight accessRight = new AccessRight();
//            return accessRight.CanChangeAuditRoleDefinition(roleId);
//        }

//        protected static bool CanChangeAuditRoleMembership(int roleId)
//        {
//            AccessRight accessRight = new AccessRight();
//            return accessRight.CanChangeAuditRoleMembership(roleId);
//        }

//        #endregion

//        #region Audit Trail Permission

//        protected static bool CanUpdateAuditTrailStatus(Guid sessionId, int auditDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.HasAuditTrailPermission(sessionId, auditDatabaseId, AuditTrailRight.UpdateStatus);
//        }

//        protected static bool CanReplayAuditTrail(Guid sessionId, int auditDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.HasAuditTrailPermission(sessionId, auditDatabaseId, AuditTrailRight.Replay);
//        }

//        protected static bool CanReadAuditTrail(Guid sessionId, int auditDatabaseId)
//        {
//            AccessRight helper = new AccessRight();
//            return helper.HasAuditTrailPermission(sessionId, auditDatabaseId, AuditTrailRight.Read);
//        }

//        #endregion

//        protected static void RaiseUnauthorizedException(string msg)
//        {
//            RaiseError(ErrorState.AccessDenied, msg);
//        }

//        protected static void RaiseInvalidDatabaseOwnerRemoveException()
//        {
//            RaiseError(ErrorState.InvalidManagerRemove, "You cannot remove database owner from user role.");
//        }

//        protected static void RaiseError(SqlException ex)
//        {
//            if (ex.Number == ExceptionNumber.RaiseErrorNumber)
//            {
//                RaiseError((ErrorState)ex.State, ex.Message);
//            }
//            else
//            {
//                RaiseError(ErrorState.EmbeddedError, string.Format("{0}, message:{1}", ex.Number, ex.Message));
//            }
//        }

//        protected static void RaiseError(ErrorState error, string msg)
//        {
//            string errorCode = ((int)error).ToString();
//            string msgMark = string.Format("'{0}'", msg.Replace("'", "''"));
//            string sql = Build("RAISERROR (", msgMark, ",16,", errorCode, ")");
//            using (SqlCommand command = CreateCommand(sql))
//            {
//                SqlContext.Pipe.ExecuteAndSend(command);
//            }
//        }

//        protected static void Execute(SqlCommand command)
//        {
//            using (SqlDataReader reader = command.ExecuteReader())
//            {
//                try
//                {
//                    SqlContext.Pipe.Send(reader);
//                }
//                catch (Exception)
//                {
//                    command.Cancel();
//                    throw;
//                }
//            }
//        }

//        protected static void Execute(SqlCommand command, CommandBehavior behavior)
//        {
//            using (SqlDataReader reader = command.ExecuteReader(behavior))
//            {
//                try
//                {
//                    SqlContext.Pipe.Send(reader);
//                }
//                catch (Exception)
//                {
//                    command.Cancel();
//                    throw;
//                }
//            }
//        }

//        public static TOutput Execute(
//            string connectionString,
//            TInput input,
//            CommandBehavior behavior,
//            IDataReaderExporter dataReaderExporter)
//        {
//            using (SqlConnection connection = new SqlConnection(connectionString))
//            {
//                connection.Open();
//                using (SqlCommand command = CreateCommand(connection, input))
//                {
//                    // Return all rows
//                    SqlDataReader reader;
//                    try
//                    {
//                        reader = command.ExecuteReader(behavior);
//                    }
//                    catch (SqlException e)
//                    {
//                        connection.Trace("Error - {0}", e.ToString());
//                        throw;
//                    }

//                    int count = 0;
//                    try
//                    {
//                        do
//                        {
//                            dataReaderExporter.PrepareExport(reader);
//                            dataReaderExporter.WriteHeader(reader);

//                            while (reader.Read())
//                            {
//                                count++;
//                                dataReaderExporter.Write(reader);
//                            }
//                        } while (reader.NextResult());
//                    }
//                    finally
//                    {
//                        command.Cancel();
//                        reader.Close();
//                    }

//                    connection.Trace("Number of rows returned: {0}", count);

//                    // After reading all the data from DataReader, return the data for output parameters
//                    List<object> ret = new List<object>();
//                    foreach (SqlParameter parameter in command.Parameters)
//                    {
//                        if (parameter.Direction == ParameterDirection.Output)
//                        {
//                            ret.Add(parameter.Value);
//                        }
//                    }

//                    return CreateVariableOutput(ret.ToArray());
//                }
//            }
//        }

//        protected static void SetInstallationLastUpdate()
//        {
//            string sql = BuildSetSystemProperty(SystemPropertyItem.LastUpdateTime, "GETDATE()", null);
//            using (SqlCommand command = CreateCommand(sql))
//            {
//                SqlContext.Pipe.ExecuteAndSend(command);
//            }
//        }

//        protected static object DeserializeBuffer(byte[] buffer)
//        {
//            MemoryStream stream = new MemoryStream(buffer);
//            BinaryFormatter formatter = new BinaryFormatter()
//            {
//                Binder = new IgnoreVersionBinder()
//            };

//            return formatter.Deserialize(stream);
//        }

//        protected static void ClearConnectionPool(string connectionString)
//        {
//            using (SqlConnection traceConnection = new SqlConnection(SqlHelper.CONTEXT_CONNECTION))
//            {
//                traceConnection.Open();
//                traceConnection.Trace("Clearing connection pool for connection string = {0}", connectionString);
//            }

//            try
//            {
//                using (SqlConnection auditStoreDatabaseConnection = new SqlConnection(connectionString))
//                {
//                    SqlConnection.ClearPool(auditStoreDatabaseConnection);
//                }
//            }
//            catch (Exception ex)
//            {
//                using (SqlConnection traceConnection = new SqlConnection(SqlHelper.CONTEXT_CONNECTION))
//                {
//                    traceConnection.Open();
//                    traceConnection.Trace("Failed in clearing connection pool for connection string = {0}: {1}", connectionString, ex.Message);
//                }
//            }
//        }

//        /// <summary>
//        /// Implementation of SerializationBinder to ignore assembly version in serialization.
//        /// </summary>
//        internal class IgnoreVersionBinder : SerializationBinder
//        {
//            /// <summary>
//            /// When overridden in a derived class, controls the binding of a serialized object to a type.
//            /// </summary>
//            /// <param name="assemblyName">Specifies the <see cref="T:System.Reflection.Assembly"/> name of the serialized object.</param>
//            /// <param name="typeName">Specifies the <see cref="T:System.Type"/> name of the serialized object.</param>
//            /// <returns>
//            /// The type of the object the formatter creates a new instance of.
//            /// </returns>
//            public override Type BindToType(string assemblyName, string typeName)
//            {
//                // Just get type by its name from current executing assembly
//                return Type.GetType(typeName);
//            }
//        }

//        /// <summary>
//        /// Build the sql statement for updating an entry in system property table
//        /// </summary>
//        /// <param name="item">the item to update</param>
//        /// <param name="value">the new value</param>
//        /// <param name="countVariable">the count variable</param>
//        /// <returns></returns>
//        protected static string BuildSetSystemProperty(SystemPropertyItem item, object value, object countVariable)
//        {
//            return BuildSetSystemProperty(item, value, countVariable, false);
//        }

//        /// <summary>
//        /// Build the SQL statement for updating an entry from system property table.
//        /// </summary>
//        /// <param name="item">The item.</param>
//        /// <param name="valueVariable">The value variable.</param>
//        /// <param name="countVariable">The count variable.</param>
//        /// <param name="encrypted">Indicate whether the stored value is encrypted.</param>
//        /// <returns>The SQL statement for updating an entry from system property table.</returns>
//        protected static string BuildSetSystemProperty(SystemPropertyItem item, object value, object countVariable, bool encrypted)
//        {
//            // For encrypted value, insert as varbinary
//            object actualValue = encrypted ? Encrypt(value) : value;
//            string sql = Build(
//                "IF NOT EXISTS (SELECT", SystemProperty.Name, "FROM", typeof(SystemProperty), "WHERE",
//                   SystemProperty.Name, "=", item, ")",
//                   "BEGIN",
//                       "INSERT INTO", typeof(SystemProperty), "(", List(SystemProperty.Name, SystemProperty.Value), ")",
//                       "SELECT", List(item, actualValue),
//                   "END",
//                "ELSE",
//                   "BEGIN",
//                       "UPDATE", typeof(SystemProperty), "SET", SystemProperty.Value, "=", actualValue,
//                       "WHERE", SystemProperty.Name, "=", item,
//                   "END");
//            if (countVariable != null)
//            {
//                sql = Build(sql, "SET", countVariable, "= @@ROWCOUNT");
//            }
//            return sql;
//        }

//        /// <summary>
//        /// Build the sql statement for getting an entry from system property table
//        /// </summary>
//        /// <param name="item">the item to get</param>
//        /// <param name="valueVariable">the value variable</param>
//        /// <param name="countVariable">the count variable</param>
//        /// <returns></returns>
//        protected static string BuildGetSystemProperty(SystemPropertyItem item, object valueVariable, object countVariable)
//        {
//            return BuildGetSystemProperty(item, valueVariable, countVariable, false);
//        }

//        /// <summary>
//        /// Build the SQL statement for getting an entry from system property table.
//        /// </summary>
//        /// <param name="item">The item.</param>
//        /// <param name="valueVariable">The value variable.</param>
//        /// <param name="countVariable">The count variable.</param>
//        /// <param name="encrypted">Indicate whether the stored value is encrypted.</param>
//        /// <returns>The SQL statement for getting an entry from system property table.</returns>
//        protected static string BuildGetSystemProperty(SystemPropertyItem item, object valueVariable, object countVariable, bool encrypted)
//        {
//            object value = encrypted ? Decrypt(ToSql("CAST(", SystemProperty.Value, "AS VARBINARY(8000))")) : ToSql(SystemProperty.Value);
//            string sql = Build(
//                "SELECT", valueVariable, "=", Convert(item, value), "FROM",
//                typeof(SystemProperty), "WHERE", SystemProperty.Name, "=", item);
//            if (countVariable != null)
//            {
//                sql = Build(sql, "SET", countVariable, "= @@ROWCOUNT");
//            }
//            return sql;
//        }

//        /// <summary>
//        /// Generate the SQL to encrypt the given vairable.
//        /// </summary>
//        /// <param name="vairable">The vairable to be encrypted.</param>
//        /// <returns>The SQL to encrypt the given vairable.</returns>
//        protected static string Encrypt(object vairable)
//        {
//            return Build("EncryptByPassPhrase(",
//                "(SELECT CAST(value as NVARCHAR) FROM sys.extended_properties WHERE name =", QuoteCharUnicode(ExtendedPropertyName.ENCRYPT_PASSPHRASE), "),",
//                vairable, ")");
//        }

//        /// <summary>
//        /// Generate the SQL to decrypt the given vairable.
//        /// </summary>
//        /// <param name="vairable">The vairable to be decrypted.</param>
//        /// <returns>The SQL to decrypt the given vairable.</returns>
//        protected static string Decrypt(object vairable)
//        {
//            return Build("DecryptByPassPhrase(",
//                "(SELECT CAST(value as NVARCHAR) FROM sys.extended_properties WHERE name =", QuoteCharUnicode(ExtendedPropertyName.ENCRYPT_PASSPHRASE), "),",
//                vairable, ")");
//        }
//    }

//    /// <summary>
//    /// Interface for data reader exporter.
//    /// </summary>
//    public interface IDataReaderExporter : IDisposable
//    {
//        /// <summary>
//        /// Prepares the export.
//        /// </summary>
//        /// <param name="dataReader">The data reader.</param>
//        void PrepareExport(SqlDataReader dataReader);

//        /// <summary>
//        /// Writes the header of the specified data reader.
//        /// </summary>
//        /// <param name="dataReader">The data reader.</param>
//        void WriteHeader(SqlDataReader dataReader);

//        /// <summary>
//        /// Writes the current record of the specified data reader.
//        /// </summary>
//        /// <param name="dataReader">The data reader.</param>
//        void Write(SqlDataReader dataReader);
//    }
//}